package com.yjhb.android.sbz.activity;

import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.dialog.ClockRegisterDialog;
import com.yjhb.android.sbz.dialog.InfoDialog;
import com.yjhb.android.sbz.entity.TodayClockParam;
import com.yjhb.android.sbz.mvp.dailysign.DailySignPresenter;
import com.yjhb.android.sbz.mvp.dailysign.DailySignView;
import com.jojo.android.zxlib.interfaces.Callback;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.tool.ZxToast;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/12 10:34
 */
public class DailySignActivity extends BaseActivity<DailySignPresenter> implements DailySignView {
    private final String TAG = "DailySignActivity";

    private final static String SIGN = "支付1元参与挑战";
    private final static String CLOCK = "立即打卡";
    private final static String WAIT = "报名成功，明天记得来打卡哦~";

    @BindView(R.id.back)
    FrameLayout back;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.depict_tv)
    TextView depictTv;
    @BindView(R.id.money_tv)
    TextView moneyTv;
    @BindView(R.id.numberOfPersons_tv)
    TextView numberOfPersonsTv;
    @BindView(R.id.button_tv)
    TextView buttonTv;
    @BindView(R.id.myRecord_tv)
    TextView myRecordTv;
    @BindView(R.id.signStatus_tv)
    TextView signStatusTv;
    @BindView(R.id.successNum_tv)
    TextView successNumTv;
    @BindView(R.id.failNum_tv)
    TextView failNumTv;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    TodayClockParam param;

    @Override
    protected DailySignPresenter initPresenter() {
        return new DailySignPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_daily_sign;
    }

    @Override
    protected void initUI() {
        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.blue_cc),
                getResources().getColor(R.color.blue_fc),
                getResources().getColor(R.color.purple_f9));
    }

    @Override
    protected void initData() {
        presenter.getJoinClockChallengeCount();
        loadingDialog.setTipsText("加载中...");
        loadingDialog.show();
    }

    @Override
    protected void regEvent() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getJoinClockChallengeCount();
            }
        });
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        refreshLayout.setRefreshing(false);
        ZxToast.showINFOBottomToast(msg);
    }


    private void update() {
        if (param != null) {
            Date date = new Date();
            moneyTv.setText(param.getMoney().toString());
            setPersonNum(param.getTodayCount());
            setSuccessNum("" + param.getClockSuccess());
            setFailNum("" + param.getClockFail());
            if (param.isTodayClockChallenge()) {
                buttonTv.setText(WAIT);
                buttonTv.setEnabled(false);
            } else if (param.isYesterdayClockChallenge() && (date.getHours() < 10 && date.getTime() > 7) && !param.isTodayIsClock()) {
                buttonTv.setText(CLOCK);
            } else {
                buttonTv.setText(SIGN);
            }
        }
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick(R.id.myRecord_tv)
    void my() {
        ZxActivityTool.skipActivity(this, MyRecordActivity.class);
    }

    @OnClick(R.id.rule_tv)
    void rule() {
        InfoDialog dialog = new InfoDialog(this);
        dialog.show();
    }

    @OnClick(R.id.button_tv)
    void button(TextView btn) {
        if (btn.getText().toString().equals(SIGN)) {
            ClockRegisterDialog dialog = new ClockRegisterDialog(this, new Callback<Map<String, Object>>() {
                @Override
                public void callback(Map<String, Object> map) {
                    presenter.joinClockChallenge((Integer) map.get("payType"), (BigDecimal) map.get("money"));
                }
            });
            dialog.show();
        } else if (btn.getText().toString().equals(CLOCK)) {
            presenter.clock();
        }
    }

    /**
     * 设置参与人数
     *
     * @param num
     */
    private void setPersonNum(String num) {
        if (TextUtils.isEmpty(num)) return;
        CharSequence charSequence = "当前有" + num + "人参与打卡挑战";
        charSequence = ZxSpanTool.ZoomSpan(charSequence, 3, 3 + num.length(), 1.5f);
        charSequence = ZxSpanTool.ColorSpan(charSequence, 3, 3 + num.length(),
                getResources().getColor(R.color.yellow_01));
        numberOfPersonsTv.setText(charSequence);
    }

    /**
     * 设置失败人数
     *
     * @param num
     */
    private void setFailNum(String num) {
        if (TextUtils.isEmpty(num)) return;
        CharSequence charSequence = num + " 失败";
        charSequence = ZxSpanTool.ColorSpan(charSequence, 0, num.length(),
                getResources().getColor(R.color.yellow_01));
        failNumTv.setText(charSequence);
    }

    /**
     * 设置成功人数
     *
     * @param num
     */
    private void setSuccessNum(String num) {
        if (TextUtils.isEmpty(num)) return;
        CharSequence charSequence = num + " 成功";
        charSequence = ZxSpanTool.ColorSpan(charSequence, 0, num.length(),
                getResources().getColor(R.color.red_22));
        successNumTv.setText(charSequence);
    }

    @Override
    public void onInfo(TodayClockParam param) {
        this.param = param;
        update();
        loadingDialog.cancel();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onSuccess(int code, String tips) {
        loadingDialog.cancel();
        ZxToast.showSuccessBottomToast(tips);
    }
}
